package cn.com.yiqi.wsc.service.sysparams.impl;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.appparam.AppParamDao;
import cn.com.yiqi.wsc.dao.appparam.entity.AppParamEntity;
import cn.com.yiqi.wsc.service.sysparams.AppParamService;
import cn.com.yiqi.wsc.service.sysparams.dataobject.AppParamDO;
import cn.com.yiqi.wsc.service.sysparams.dataobject.DistributIonDO;
import cn.com.yiqi.wsc.service.sysparams.dataobject.PointGetRuleDO;
import cn.com.yiqi.wsc.service.task.BirthDayRemainderTask;
import cn.com.yiqi.wsc.service.user.UserService;
import cn.com.yiqi.wsc.util.AppParamUtil;
import cn.com.yiqi.wsc.util.GsonUtil;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author 杨贵森
 * @ClassName:AppParamServiceImpl
 * @Title: AppParamServiceImpl
 * @Description: TODO
 * @date 2017/3/15
 */
@Service
public class AppParamServiceImpl implements AppParamService {

    private static final String POINT_GET_RULE = "POINT_GET_RULE";

    @Autowired
    AppParamDao appParamDao;

    @Autowired
    UserService  userService;
    @Autowired
    BirthDayRemainderTask birthDayRemainderTask;
    @Override
    public Result getMarryAppParamList(AppParamEntity appParamEntity) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        AppParamDO appParamDO = AppParamUtil.changeAPLToAPD(appParamDao.getMarryParamList(appParamEntity));
        PointGetRuleDO pointGetRuleDO = getPointGetRule();
        appParamDO.setPointGetRuleDO(pointGetRuleDO);
        result.putData("appParam", appParamDO);
        return result;
    }

    @Override
    public Result getDistributionParamList(AppParamEntity appParamEntity) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        List<DistributIonDO> distributionParamList = getDistributionDOs(appParamEntity);
        result.putData("distributionList", distributionParamList);
        return result;
    }

    @Override
    public List<DistributIonDO> getDistributionDOs(AppParamEntity appParamEntity) {

        List<AppParamEntity> appParamEntityList = appParamDao.getMarryParamList(appParamEntity);
        String str = appParamEntityList.get(0).getValue();
        String[] distributionParam = str.substring(0,str.length()-1).split(",");
        List<DistributIonDO> distributionParamList = new ArrayList<>();
        for (int i = 0 ; i < distributionParam.length ; i++ ){
            String[] dis = distributionParam[i].split(":");
            DistributIonDO appParam= new DistributIonDO();
            appParam.setIndex(Integer.parseInt(dis[0]));
            appParam.setRate(Double.parseDouble(dis[1]));
            appParam.setType(Integer.parseInt(dis[2]));
            distributionParamList.add(appParam);
        }
        return distributionParamList;
    }

    @Override
    public PointGetRuleDO getPointGetRule() {
        AppParamEntity appParamEntity = appParamDao.getByCode(POINT_GET_RULE);
        PointGetRuleDO pointGetRuleDO = null;
        if (null == appParamEntity) {
            pointGetRuleDO = new PointGetRuleDO();
            pointGetRuleDO.setSendPoint(false);
            return pointGetRuleDO;
        }
        pointGetRuleDO = GsonUtil.getGson().fromJson(appParamEntity.getValue(), PointGetRuleDO.class);
        return pointGetRuleDO;
    }

    @Override
    public Result updatePointGetRule(PointGetRuleDO pointGetRuleDO) {
        Result result = new Result();
        if (null == pointGetRuleDO) {
            pointGetRuleDO = new PointGetRuleDO();
            pointGetRuleDO.setSendPoint(false);
        }
        if (!pointGetRuleDO.isSendPoint()) {
            pointGetRuleDO.setPointRate(null);
            pointGetRuleDO.setVipPointRate(null);
        }
        AppParamEntity appParamEntity = appParamDao.getByCode(POINT_GET_RULE);
        if (null == appParamEntity) {
            appParamEntity = new AppParamEntity();
            appParamEntity.setCode(POINT_GET_RULE);
            appParamEntity.setName("平台积分获得规则");
            appParamEntity.setValue(GsonUtil.getGson().toJson(pointGetRuleDO));
            appParamEntity.setInsertTime(new Date());
            appParamDao.insert(appParamEntity);
            return ResultUtil.success(result);
        }
        appParamEntity.setValue(GsonUtil.getGson().toJson(pointGetRuleDO));
        appParamEntity.setUpdateTime(new Date());
        appParamDao.update(appParamEntity);
        return ResultUtil.success(result);
    }


    @Override
    @Transactional
    public Result updateAppParam(AppParamDO appParamDO) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        List<AppParamEntity> appParamEntityList = AppParamUtil.productSYSAppParamEntity(appParamDO);
             for(AppParamEntity  appParam: appParamEntityList ){
                 appParam.setUpdateUserId(userService.getCurrentUserId());
                 if(appParam.getCode().equals("BIRTHDAY_REMINDTIME")){

                     String[] time=  appParam.getValue().split(":");
                     Integer hour =Integer.parseInt(time[0]);
                     Integer miniter=Integer.parseInt(time[1]);

                     if( Integer.parseInt(time[0].substring(0,1))==0){
                         hour=Integer.parseInt(time[0].substring(1));
                     }

                     if(Integer.parseInt(time[1].substring(0,1))==0){
                         miniter=Integer.parseInt(time[1].substring(1));
                     }

                     birthDayRemainderTask.updateReminderTask(hour,miniter);
                 }
                 result.putData("updateStatus_"+appParam.getCode(), appParamDao.updateAppParam(appParam));
             }
        return result;
    }

    @Override
    @Transactional
    public Result updateDistributionAppParam(List<DistributIonDO> distributionList) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        StringBuilder str = new StringBuilder();
        for (DistributIonDO appParamDO : distributionList){
            str.append(appParamDO.getIndex()+":"+appParamDO.getRate()+":"+appParamDO.getType()+",");
        }

        AppParamEntity appParamEntity = new AppParamEntity();
        appParamEntity.setCode("DISTRIBUTION");
        appParamEntity.setUpdateUserId(userService.getCurrentUserId());
        appParamEntity.setValue(str.toString());
        result.putData("updateStatus_"+appParamEntity.getCode(),  appParamDao.updateDistributionAppParam(appParamEntity));
        return result;
    }

}
